package com.example.eldersystem.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.model.Event;
import com.example.eldersystem.operatedata.service.EventService;

@Configuration
@EnableScheduling
public class ScheduleTask {

    private int num=0;
    @Autowired
    EventService eventService;
    //发送消息的模板类
    //@Autowired
    //SimpMessagingTemplate simpMessagingTemplate;
    //3.添加定时任务
    @Scheduled(cron = "0/2 * * * * ?")
    @Transactional
    void configureTasks() {

        int numNow=eventService.getEventNum();
        System.out.println("现在的事件数量："+numNow);
        if(numNow<=num){ return; }
        num=numNow;

        System.err.println("执行定时任务时间: " + LocalDateTime.now());

        WebSocket ws = new WebSocket();
        try {
            //ws.sendMessageAll(JSON.toJSONString(username));
            ws.sendMessageTo("报表有新数据了","wanglei");
            //System.out.println(ws.onMessage(""));
            //ws.onMessage(e);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
